<?php

/**
 * CmppDeliver
 *
 * @author  yyc
 * @version 1.0
 * @package main
 * @date 2017-06-12 0012
 * @time 11:26:39
 */

/**
 *
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

namespace cmpp;

use cmpp\CmppResp;
use core\Bytes;
class CmppDeliver extends CmppResp
{

    public function deliver($data,$cli){

        //$data = self::unPack($data);
        $length = 8+21+10+1+1+1+32+1+1+1;
        $total_length = strlen($data);
        $param = substr($data,0,$length);
        $rs = $rs1 = $rs2 = [];

        $rs1 = self::unParamPack($param);
        $rs1['DestId'] = self::getString($rs1['DestId']);
        $rs1['ServiceId'] = self::getString($rs1['ServiceId']);
        $rs1['SrcTerminalId'] = self::getString($rs1['SrcTerminalId']);
        
        L('CmppDeliver')->info('deliver success',[$rs]);
        if($rs1['RegisteredDelivery'] === 1){
            $statContent = substr($data,$length,$total_length - 20);
            $rs2 = self::unStatPack($statContent);
            $rs2['Stat'] = self::getString($rs2['Stat']);
            $rs2['SubmitTime'] = self::getString($rs2['SubmitTime']);
            $rs2['DoneTime'] = self::getString($rs2['DoneTime']);
            $rs2['DestTerminalId'] = self::getString($rs2['DestTerminalId']);

            R()->lpush(C()->get('key.stat'),JSON($rs2));
        }else{
            $rs2 = self::unContentPack(substr($data,$length));
            $rs2['MsgContent'] = encod($rs2['MsgContent'],'GBK','UTF-8');
            //$rs2['LinkID'] =  substr($rs2['LinkID'],0,strlen($rs2['LinkID'])-1).ord(substr($rs2['LinkID'],-1));
            $rs = array_merge($rs1,$rs2);
            R()->lpush(C()->get('key.up'),JSON($rs));
        }
        $msg = \cmpp\CmppSend::deliverResp($rs1['MsgId'],0);
        $cli->send($msg);
        unset($rs,$rs1,$rs2);

    }

    public static function unParamPack($data){
        //$length = 8+21+10+1+1+1+32+1+1+1;
        $key = "PMsgId/a21DestId/a10ServiceId/cTPpid/cTPudhi/cMsgFmt/a32SrcTerminalId/cSrcTerminalType/cRegisteredDelivery/cMsgLength";
        //$key = "QMsgId/a21DestId/a10ServiceId/cTPpid/cTPudhi/cMsgFmt/a32SrcTerminalId/cSrcTerminalType/cRegisteredDelivery/cMsgLength";
        $body = unpack($key,$data);
        return $body;
    }

    public static function unContentPack($data){
        $length = strlen($data) - 20;
        $key = "a".$length."MsgContent/a20LinkID";
        //$key = "QMsgId/a21DestId/a10ServiceId/cTPpid/cTPudhi/cMsgFmt/a32SrcTerminalId/cSrcTerminalType/cRegisteredDelivery/cMsgLength";
        $body = unpack($key,$data);
        return $body;
    }

    public static function unStatPack($data){
        $key = "PMsgId/a7Stat/a10SubmitTime/a10DoneTime/a32DestTerminalId/NSMSCsequence";
        $body = unpack($key,$data);
        return $body;
    }

    public static function getString($string){
        if(!$string){
            return '';
        }
        return Bytes::toStr(self::getStringFromByte(Bytes::getBytes($string)));
    }

    public static function getStringFromByte($bytes){
        $data = [];
        foreach ($bytes as $val){
            if($val === 0){
                break;
            }
            $data[] = $val;
        }
        return $data;
    }
}